package hibernate.web;

import hibernate.fileControler.CheckFileHash;
import hibernate.fileControler.MyException;
import hibernate.model.Aygit;
import hibernate.model.Resimler;
import hibernate.model.Sirketprofil;
import hibernate.model.Tv;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("TvBean")
@Scope("session")
public class TvBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Tv selectedTv;	
	private List<Tv> tvList;
	private List<Resimler> selectedTvResimList;
	private Resimler selectedTvResim;
	
	private SirketprofilBean firmaBean;
	private ResimlerBean resimBean;
	private ArrayList<UploadedFile> fileList = new ArrayList<UploadedFile>();
	
	@Override
	@PostConstruct
	public void init() {
	}

	@Override
	public String restart() {
		init();
		return "";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedTv);
		init();
		return "";
	}

	@Override
	public String viewNew() {
		System.out.println("Tv viewNew");
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.setYeniUrun(true);
		Sirketprofil srf = ((SirketprofilBean) emmyService.getBean(new SirketprofilBean())).getCurrentSirketpro();
		Aygit agt = new Aygit(srf,"TV","","");
					
		selectedTv = new Tv();
		selectedTv.setAygit(agt);
		selectedTv.setSirketprofil(srf);
		agtBean.setSelectedAygitObject(selectedTv);
		agtBean.setEdit(true);
		return "";
	}

	@Override
	public String viewDetail() {/*
		List<Tv> valueList = (List<Tv>) tvTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = tvTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentTv= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentTv.getAygit());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}

	@SuppressWarnings("unchecked")
	public String getDetayliTv(){		
		tvList = (List<Tv>) emmyService.getQuery(createQuery() );
		return "list";
	}
	
	@Override
	public String SaveOrUpdate() throws MyException {
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean() );
		agtBean.setSelectedAygitObject(selectedTv);
		resimBean = (ResimlerBean) emmyService.getBean(new ResimlerBean());
		try{
		if(resimBean.SaveOrUpdate().equals("OK")){
			fileList = resimBean.getFileList();
			Set<Resimler> resCollec = new HashSet<Resimler>();
			for(int i=0;i<fileList.size(); i++){
				Resimler res = new Resimler();
				res.setResimKonum("../productImages/Tv/"+
				( CheckFileHash.fileCheck(fileList.get(i))+fileList.get(i).getFileName() ).toLowerCase() );
				res.setAygit(selectedTv.getAygit());
				resCollec.add(res);	
				System.out.println(resCollec.size());
			}
			selectedTv.getAygit().setResimlers(resCollec);
			emmyService.saveOrUpdate(selectedTv);
			agtBean.deleteSelectedAygit();
			emmyService.clearSession(resimBean);
		}
		}catch(Exception e){
			System.out.println("Tv Save error : "+e);
		}
		return "";
	}

	@Override
	public String createQuery() {
		Tv tv  = selectedTv;		
		String query = "from Tv";				
		
		if(tv == null){
			return query;
		}
		if(tv.getSirketprofil().getFirmaid() != 0){
			if(query.indexOf("where") != -1){
				query += " and ureticiid="+tv.getSirketprofil().getFirmaid()+"";
				
			}
			else{
				query += " where ureticiid="+tv.getSirketprofil().getFirmaid()+"";			
			}
		}
		if(tv.getEkranBoyutu() != null){
			if(query.indexOf("where") != -1){
				query += " and ekranBoyutu like '%"+tv.getEkranBoyutu()+"%'";
				
			}
			else{
				query += " where ekranBoyutu like '%"+tv.getEkranBoyutu()+"%'";			
			}
		}
		if(tv.getHd() != null){
			if(query.indexOf("where") != -1){
				query += " and hd like '%"+tv.getHd()+"%'";
				
			}
			else{
				query += " where hd like '%"+tv.getHd()+"%'";			
			}
		}
		if(tv.getHdmi() != null){
			if(query.indexOf("where") != -1){
				query += " and hdmi like '%"+tv.getHdmi()+"%'";
				
			}
			else{
				query += " where hdmi like '%"+tv.getHdmi()+"%'";			
			}
		}
		if(tv.getScart() != null){
			if(query.indexOf("where") != -1){
				query += " and scart like '%"+tv.getScart()+"%'";
				
			}
			else{
				query += " where scart like '%"+tv.getScart()+"%'";			
			}
		}
		return query;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public Tv getSelectedTv() {
		return selectedTv;
	}

	public void setSelectedTv(Tv selectedTv) {
		this.selectedTv = selectedTv;
	}

	public List<Tv> getTvList() {
		return tvList;
	}

	public void setTvList(List<Tv> tvList) {
		this.tvList = tvList;
	}

	public List<Resimler> getSelectedTvResimList() {
		return selectedTvResimList;
	}

	public void setSelectedTvResimList(List<Resimler> selectedTvResimList) {
		this.selectedTvResimList = selectedTvResimList;
	}

	public Resimler getSelectedTvResim() {
		return selectedTvResim;
	}

	public void setSelectedTvResim(Resimler selectedTvResim) {
		this.selectedTvResim = selectedTvResim;
	}

	public SirketprofilBean getFirmaBean() {
		return firmaBean;
	}

	public void setFirmaBean(SirketprofilBean firmaBean) {
		this.firmaBean = firmaBean;
	}

	public ResimlerBean getResimBean() {
		return resimBean;
	}

	public void setResimBean(ResimlerBean resimBean) {
		this.resimBean = resimBean;
	}

	public ArrayList<UploadedFile> getFileList() {
		return fileList;
	}

	public void setFileList(ArrayList<UploadedFile> fileList) {
		this.fileList = fileList;
	}
}